package com.swanclouds.redfish.managers;

import com.google.common.base.Preconditions;
import com.swanclouds.api.managers.SecurityService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.Payload;
import com.swanclouds.model.managers.GenerateCSR;
import com.swanclouds.redfish.internal.BaseManagerService;
import com.swanclouds.redfish.managers.domain.HttpsCertModel;
import com.swanclouds.redfish.managers.domain.SecurityServiceModel;

public class SecurityServiceImpl extends BaseManagerService implements SecurityService {

    /**
     * 查询安全服务信息
     * @return SecurityServiceModel
     */
    @Override
    public SecurityServiceModel get() {
        return get(SecurityServiceModel.class,uri(ClientConstants.PATH_MANAGERS_SECURITYSERVICE,getManagerId())).execute();
    }

    /**
     * 查询安全服务详情信息
     * @return HttpsCertModel
     */
    @Override
    public HttpsCertModel detail() {
       return get(HttpsCertModel.class,uri(ClientConstants.PATH_MANAGERS_SECURITYSERVICE_HTTPSCERT,getManagerId())).execute();
    }

    /**
     * 生成SSL证书资源信息
     * @param generateCSR 生成SSL证书资源信息参数
     * @return HttpsCertModel
     */
    @Override
    public HttpsCertModel generateCSR(GenerateCSR generateCSR) {
        Preconditions.checkNotNull(generateCSR.getCommonName(),"commonName is null");
        Preconditions.checkNotNull(generateCSR.getCity(),"city is null");
        Preconditions.checkNotNull(generateCSR.getCountry(),"country is null");
        Preconditions.checkNotNull(generateCSR.getEmailId(),"emailId is null");
        Preconditions.checkNotNull(generateCSR.getOrganization(),"organization is null");
        Preconditions.checkNotNull(generateCSR.getOrganizationUnit(),"organizationUnit is null");
        Preconditions.checkNotNull(generateCSR.getState(),"state is null");
        Preconditions.checkNotNull(generateCSR.getValidDays(),"validDays is null");
        return post(HttpsCertModel.class,uri(ClientConstants.PATH_MANAGERS_SECURITYSERVICE_HTTPSCERT_GENERATECSR,getManagerId())).entity(generateCSR).execute();
    }

    /**
     * 上传SSL证书
     * @param payload 文件上传
     * @return ActionResponse
     */
    @Override
    public ActionResponse ImportServerCertificate(Payload payload) {
        return post(ActionResponse.class,uri(ClientConstants.PATH_MANAGERS_SECURITYSERVICE_HTTPSCERT_IMPORTSERVICECERTIFICATE,getManagerId()))
                .header(ClientConstants.HEADER_CONTENT_TYPE,ClientConstants.MULTIPART_FORM_DATA)
                .entity(payload)
                .execute();
    }
}
